/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JProductTableView.java
 *
 * Created on 2010-6-5, 9:55:58
 */

package cn.ekuma.epos.analysis.front.product;

import cn.ekuma.data.dao.bean.I_ViewBean;
import cn.ekuma.data.ui.swing.AbstractDTOTableModel;
import cn.ekuma.data.ui.swing.dnd.BeanListTransferable;
import cn.ekuma.epos.analysis.front.product.model.ProductPosSellAnalysisTableModel;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.qkorder.link.JOperationProductCategoryPanel;
import cn.ekuma.epos.qkorder.link.JProductBomIncludeStorePanel;
import cn.ekuma.epos.qkorder.link.JProductIncludeBomStorePanel;
import cn.ekuma.epos.qkorder.link.JSimilarProductStorePanel;
import cn.ekuma.util.SwingUtil;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.panels.JDropProductByLocationLayoutPanel;
import com.openbravo.pos.panels.JDropProductByLocationStatePanel;
import com.openbravo.pos.panels.JDropProductByOperationCategoryPanel;
import com.openbravo.pos.panels.event.BaseBeanChangedListener;
import com.openbravo.pos.panels.event.SellProductChangedListener;
import com.openbravo.pos.panels.link.JBomProductForIncludePanel;
import com.openbravo.pos.panels.link.JCustomerStorePanel;
import com.openbravo.pos.panels.link.JProductLotNoStorePanel;
import com.openbravo.pos.panels.link.JProductStoreDiaryPanel;
import com.openbravo.pos.panels.link.JProductStorePanel;
import com.openbravo.bean.erp.viewbean.FrontProductAnalysisInfo;
import com.openbravo.data.loader.KeyBuilder;

import java.awt.Component;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ListSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JPanel;
import javax.swing.BoxLayout;

import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.table.model.RowNumTableModelWapper;

/**
 *
 * @author Administrator
 */
public class JProductTableView extends javax.swing.JPanel implements
DragGestureListener, // For recognizing the start of drags
DragSourceListener{
    AnalysisModelManager manager;
    private FrontProductAnalysisInfo product;
	private JDropProductByOperationCategoryPanel dropPanel;
	private JDropProductByLocationStatePanel jDropProductByLocationStatePanel;
	ProductPosSellAnalysisTableModel tableModel;
	
    /** Creates new form JProductTableView */
    @SuppressWarnings("serial")
	public JProductTableView(final AnalysisModelManager manager) {
        this.manager=manager;
        initComponents();
        tableModel=manager.tableModel;
        jXTable1.setModel(new RowNumTableModelWapper(manager.tableModel));
        jXTable1.setColumnControlVisible(true);
        jXTable1.setAutoCreateRowSorter(true);
        jXTable1.setAutoResizeMode(JXTable.AUTO_RESIZE_OFF);
        jXTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jXTable1.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent e) {
               int index=jXTable1.getSelectedRow();
               if(index!=-1){
                   index=jXTable1.convertRowIndexToModel(index);
                   FrontProductAnalysisInfo prodAnalysis=tableModel.getObj(index);
                   manager.fireProductChanged(prodAnalysis.getM_ID(),prodAnalysis);
                   fireProductChanged(prodAnalysis);
               }
            }
        });
        
        DragSource dragSource = DragSource.getDefaultDragSource();
	    dragSource.createDefaultDragGestureRecognizer(jXTable1,
	        DnDConstants.ACTION_COPY_OR_MOVE, this);
        
	    
	    
        jTabbedPane1.addTab(AppLocal.getIntString("label.prodstock"), new JProductStorePanel(manager.getDlSales()));
        jTabbedPane1.addTab(AppLocal.getIntString("label.simialProdSell"), new JSimialProductPosSellPanel((I_DataLogicERP) manager.getDlSales()));
        jTabbedPane1.addTab(AppLocal.getIntString("label.SimilarProductStore"), new JSimilarProductStorePanel(manager.getDlSales()));
        jTabbedPane1.addChangeListener(new ChangeListener(){
            @Override
            public void stateChanged(ChangeEvent e) {
                if(product!=null)
                   fireProductChanged(product);
            }
        });
        
        jTabbedPane1.addTab(AppLocal.getIntString("label.operationProductCategory"), new JOperationProductCategoryPanel(manager.getDlSales()));
        
        dropPanel=new JDropProductByOperationCategoryPanel(manager.getDlSales());
        jTabbedPane1.addTab(AppLocal.getIntString("label.operationProductCategory"), null, dropPanel, null);
        
        JProductBomIncludeStorePanel productBomIncludeStorePanel=new JProductBomIncludeStorePanel(manager.getDlSales());
        jTabbedPane1.addTab(AppLocal.getIntString("label.prodBomIncludeStock"), productBomIncludeStorePanel);
        
        JProductIncludeBomStorePanel productIncludeBomStorePanel=new JProductIncludeBomStorePanel(manager.getDlSales());
        jTabbedPane1.addTab(AppLocal.getIntString("label.prodIncludeBomStock"), productIncludeBomStorePanel);
        
        JProductLotNoStorePanel productLotNoStorePanel=new JProductLotNoStorePanel(manager.getDlSales());
        jTabbedPane1.addTab(AppLocal.getIntString("label.lotNoStore"), productLotNoStorePanel);
        
        JBomProductForIncludePanel jBomProductForIncludePanel=new JBomProductForIncludePanel(manager.getDlSales());
        jTabbedPane1.addTab(AppLocal.getIntString("label.prodIncludeBom"),jBomProductForIncludePanel);
        
        JCustomerStorePanel customerNoStorePanel=new JCustomerStorePanel(manager.getDlSales());
        jTabbedPane1.addTab(AppLocal.getIntString("label.customerStore"), customerNoStorePanel);
        
        jTabbedPane1.addTab(AppLocal.getIntString("label.locationLayout"), new JDropProductByLocationLayoutPanel(manager.getDlSales()));

        jDropProductByLocationStatePanel=new JDropProductByLocationStatePanel(manager.getDlSales());
        jTabbedPane1.addTab(jDropProductByLocationStatePanel.getTabName(),jDropProductByLocationStatePanel);  
        manager.addLocationChangedListener(jDropProductByLocationStatePanel);
        
       JProductStoreDiaryPanel jProductStoreDiaryPanel=new JProductStoreDiaryPanel(manager.getDlSales());
       jTabbedPane1.addTab(AppLocal.getIntString("label.productStoreDiary"), jProductStoreDiaryPanel);
       manager.addLocationChangedListener(jProductStoreDiaryPanel);
    }
    

     public void peferredJXTableColumn(){
         SwingUtil.PreferredJXTableColumn(jXTable1);
     }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jXTable1 = new org.jdesktop.swingx.JXTable();
        jTabbedPane1 = new javax.swing.JTabbedPane();

        setLayout(new java.awt.BorderLayout());

        jSplitPane1.setDividerLocation(600);
        jSplitPane1.setDividerSize(10);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane1.setOneTouchExpandable(true);

        jScrollPane1.setPreferredSize(new java.awt.Dimension(302, 600));

        jXTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jXTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jXTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jXTable1);

        jSplitPane1.setTopComponent(jScrollPane1);
        jSplitPane1.setRightComponent(jTabbedPane1);

        add(jSplitPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void jXTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jXTable1MouseClicked
        if(evt.getClickCount()==2){
            int index=jXTable1.getSelectedRow();
              if(index!=-1){
                   index=jXTable1.convertRowIndexToModel(index);
                   FrontProductAnalysisInfo prodAnalysis=tableModel.getObj(index);
                   manager.fireDialyProductPropertyChanged(AnalysisModelManager.PROPERTY_DialyProductID,prodAnalysis.getM_ID(),prodAnalysis.getM_sName());
            }
        }
    }//GEN-LAST:event_jXTable1MouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private org.jdesktop.swingx.JXTable jXTable1;
    private JPanel panel;
    // End of variables declaration//GEN-END:variables

    private void fireProductChanged(FrontProductAnalysisInfo prodAnalysis) {
        this.product=prodAnalysis;
        Component selectTab=jTabbedPane1.getSelectedComponent();
        if(selectTab!=null&&selectTab instanceof SellProductChangedListener){
        	SellProductChangedListener listener=(SellProductChangedListener)selectTab;
        	listener.baseBeanChanged(manager.getSelectLoction(), prodAnalysis.getM_ID(), manager.getStartDate(), manager.getEndDate());
        }
        if(selectTab!=null&&selectTab instanceof BaseBeanChangedListener){
        	BaseBeanChangedListener linstener=(BaseBeanChangedListener)selectTab;
               linstener.baseBeanChanged(KeyBuilder.getKey(prodAnalysis.getM_ID()),null);
        }
    }


	@Override
	public void dragEnter(DragSourceDragEvent dsde) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void dragOver(DragSourceDragEvent dsde) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void dropActionChanged(DragSourceDragEvent dsde) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void dragExit(DragSourceEvent dse) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void dragDropEnd(DragSourceDropEvent dsde) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void dragGestureRecognized(DragGestureEvent dge) {
		int[] selectedRows=jXTable1.getSelectedRows();
        List<I_ViewBean> rows=new ArrayList();
        for(int i=0;i<selectedRows.length;i++){
            rows.add((FrontProductAnalysisInfo) tableModel.getObj(jXTable1.convertRowIndexToModel(selectedRows[i])));
        }
	    Transferable transferable = new BeanListTransferable(rows);
	    dge.startDrag(null, transferable, this);
	}
}
